package cn.com.w.dao.Service.Impl;


import cn.com.w.common.DTO.SysUserDTO;
import cn.com.w.common.Utils.PasswordUtils;
import cn.com.w.common.VO.BaseVO;
import cn.com.w.dao.Entity.SysUser;
import cn.com.w.dao.Entity.SysUserPassword;
import cn.com.w.dao.Entity.SysUserRole;
import cn.com.w.dao.Mapper.SysRoleMapper;
import cn.com.w.dao.Mapper.SysUserMapper;
import cn.com.w.dao.Mapper.SysUserPasswordMapper;
import cn.com.w.dao.Mapper.SysUserRoleMapper;
import cn.com.w.dao.Service.ISysUserService;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author wangJK
 * @since 2025-04-25
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysUserPasswordMapper sysUserPasswordMapper;

    @Override
    public void saveOne(SysUserDTO user) {
        SysUser sysUser = SysUser.builder()
                .phoneNumber(user.getPhoneNumber())
                .email(user.getEmail())
                .wechatId(user.getWechat())
                .username(user.getUsername())
                .status(user.getStatus())
                .passwordNonExpired(user.getPasswordNonExpired())
                .build();
        int isTrue = sysUserMapper.insert(sysUser);
        if (isTrue > 0) {
            sysUserPasswordMapper.insert(SysUserPassword.builder()
                    .userId(sysUser.getId())
                    .password(PasswordUtils.encryptionPassword(user.getPassword()))
                    .build());
            sysUserRoleMapper.insert(SysUserRole.builder()
                    .enabled(true)
                    /*
                     * 普通用户权限
                     */
                    .roleId(2L)
                    .userId(sysUser.getId())
                    .build());

        }
    }

    @Override
    public SysUserDTO loadUserByUsername(String username) throws Exception {
        if (StringUtils.isBlank(username)) {
            return new SysUserDTO();
        }
        SysUser user = sysUserMapper.getByUsername(username);
        if (user == null) {
            BaseVO.error(username + "用户未注册！");
        }
        SysUserDTO dto = SysUserDTO.builder()
                .id(user.getId())
                .username(user.getUsername())
                .phoneNumber(user.getPhoneNumber())
                .email(user.getEmail())
                .wechat(user.getWechatId())
                .status(user.getStatus())
                .passwordNonExpired(user.getPasswordNonExpired())
                .build();
        if (dto == null) {
            return new SysUserDTO();
        }
        String password = sysUserPasswordMapper.getPasswordById(dto.getId());
        dto.setPassword(password);
        // 获取用户关联的所有角色
        dto.setRoles(sysRoleMapper.listAllByUserId(dto.getId()));
        return dto;
    }
}
